<?php 
/*
 module:		关注企业记录
 create_time:	2024-03-12 17:33:44
 author:		
 contact:		
*/

namespace app\api\controller;

use app\api\service\PersonalfocuscompanyService;
use app\api\model\Personalfocuscompany as PersonalfocuscompanyModel;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;

class Personalfocuscompany extends Common {


 /*start*/
    /**
     * @api {get} /Personalfocuscompany/index 01、首页数据列表
     */
    function index()
    {
        $limit = $this->request->post('limit', 20, 'intval');
        $page = $this->request->post('page', 1, 'intval');
        $where = [];
        $where['a.uid'] =  $this->request->uid;
        $field='a.id,a.company_id,b.companyname,b.nature_cn,b.trade_cn,b.district_cn,
		b.street_cn,b.scale_cn,b.registered,b.currency,b.address,b.contact,b.telephone,b.landline_tel,b.email,b.website,
	    b.logo,b.refreshtime,b.tag';
        $orderby = 'a.id desc';

        $res = PersonalfocuscompanyModel::alias('a')
            ->join('company_profile b','a.company_id=b.id')
            ->where(formatWhere($where))->field($field)
            ->order($orderby)->paginate(['list_rows'=>$limit,'page'=>$page])
            ->toArray();
        if (!empty($res["data"])) {
            $list = $res["data"];
            foreach ($list as &$item) {
                $item["refreshtime"] = timeAnalysis2($item["refreshtime"]);
                if (empty($item["tag"]) || $item["tag"] == "请选择") {
                    $item["tag"] = [];
                } else {
                    $item["tag"] = explode(",", $item["tag"]);
                }
                if (!empty($item["contents"])) {
                    $item["contents"] = htmlspecialchars_decode($item["contents"]);
                }
                if (!empty($item["logo"])) {
                    $item["logo"] = "https://www.lianhezhaopin.com/data/upload/company_logo/". $item["logo"];
                }
            }
            $res["data"] = $list;
        }
        return $this->ajaxReturn($this->successCode, '返回成功', htmlOutList($res));
    }
    /**
     * @api {post} /Personalfocuscompany/collection 02、关注
     */
    function collection()
    {
        $postField = 'company_id';
        $data = $this->request->only(explode(',', $postField), 'post', null);
        if (empty($data['company_id'])) {
            throw new ValidateException('参数错误');
        }
        $uid = $this->request->uid;
        $where['company_id'] = $data['company_id'];
        $where['uid'] = $uid;
        $info = PersonalfocuscompanyModel::where($where)->findOrEmpty()->toArray();
        if (empty($info)) {
            $data['uid'] = $uid;
            $data['addtime'] = time();
            $res = PersonalfocuscompanyModel::insertGetId($data);
        } else {
            $res = PersonalfocuscompanyModel::where($where)->delete();
        }
        return $this->ajaxReturn($this->successCode, '操作成功', $res);
    }
    /*end*/



}

